﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace HuynhQuangWebsite.Models
{
     [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "Nhập lại mật khẩu không khớp.")]
    public partial class Admin
    {
        [Required]
        [DataType(DataType.Password)]
        [DisplayName("Confirm password")]
        public string ConfirmPassword { get; set; }
         //
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

         public class Admin_Validation
         {
             [Required]
             [DisplayName("User name")]
              public string UserName { get; set; }

             [Required]
             [ValidatePasswordLength]
             [DataType(DataType.Password)]
             public String Password { get; set; }
             //
          
         }
        [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
        public sealed class PropertiesMustMatchAttribute : ValidationAttribute
        {
            private const string _defaultErrorMessage = "'{0}' and '{1}' do not match.";
            private readonly object _typeId = new object();

            public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty)
                : base(_defaultErrorMessage)
            {
                OriginalProperty = originalProperty;
                ConfirmProperty = confirmProperty;
            }

            public string ConfirmProperty { get; private set; }
            public string OriginalProperty { get; private set; }

            public override object TypeId
            {
                get
                {
                    return _typeId;
                }
            }

            public override string FormatErrorMessage(string name)
            {
                return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                OriginalProperty, ConfirmProperty);
            }

            public override bool IsValid(object value)
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
                object originalValue = properties.Find(OriginalProperty, true /* ignoreCase */).GetValue(value);
                object confirmValue = properties.Find(ConfirmProperty, true /* ignoreCase */).GetValue(value);
                return Object.Equals(originalValue, confirmValue);
            }
        }

        [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
        public sealed class ValidatePasswordLengthAttribute : ValidationAttribute
        {
            private const string _defaultErrorMessage = "'{0}' must be at least {1} characters long.";
            private readonly int _minCharacters = Membership.Provider.MinRequiredPasswordLength;

            public ValidatePasswordLengthAttribute()
                : base(_defaultErrorMessage)
            {
            }

            public override string FormatErrorMessage(string name)
            {
                return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString,
                name, _minCharacters);
            }

            public override bool IsValid(object value)
            {
                string valueAsString = value as string;
                return (valueAsString != null && valueAsString.Length >= _minCharacters);
            }
        }
    }
    }
